package com.casstime.ec.cloud.meeting.web.ftl.template;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class MarketCurrency implements TemplateDirectiveModel{

	@SuppressWarnings("rawtypes")
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		DecimalFormat myformat = new DecimalFormat();
		myformat.applyPattern("##,##0.00");
		Object o = params.get("amount");
        if (o == null) {
            o = 0.00;
        }
        BigDecimal amount = BigDecimal.ZERO;
        if (o instanceof SimpleScalar) {
            SimpleScalar s = (SimpleScalar) o;
            amount = new BigDecimal(s.getAsString().replaceAll(",", ""));
        }else{
        	amount = new BigDecimal(o.toString().replaceAll(",", ""));
        }
		env.getOut().write(myformat.format(amount.setScale(2, BigDecimal.ROUND_HALF_UP)).toString());
	}
}
